﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaPortal.DeployTool;
using MediaPortal.DeployTool.InstallationChecks;

namespace Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn.Wizards.InstallTVServer
{
    using Utils = MediaPortal.DeployTool.Utils;
    using CheckState = MediaPortal.DeployTool.CheckState;

    public partial class WPInstallationControl : UserControl
    {
        public WPInstallationControl()
        {
            InitializeComponent();

            listView.Columns[0].Text = Localizer.GetBestTranslation("Install_colApplication");
            listView.Columns[1].Text = Localizer.GetBestTranslation("Install_colState");
            listView.Columns[2].Text = Localizer.GetBestTranslation("Install_colAction");
        }

        private void AddPackageToListView(IInstallationPackage package)
        {
            listView.SmallImageList = iconsList;
            ListViewItem item = listView.Items.Add(package.GetDisplayName());
            item.Tag = package;
            CheckResult result = package.CheckStatus();
            switch (result.state)
            {
                case CheckState.INSTALLED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateInstalled"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionNothing"));
                    item.ImageIndex = 0;
                    break;
                case CheckState.NOT_INSTALLED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateNotInstalled"));
                    if (result.needsDownload)
                        item.SubItems.Add(Localizer.GetBestTranslation("Install_actionDownloadInstall"));
                    else
                        item.SubItems.Add(Localizer.GetBestTranslation("Install_actionInstall"));
                    item.ImageIndex = 1;
                    break;
                case CheckState.CONFIGURED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateConfigured"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionNothing"));
                    item.ImageIndex = 0;
                    break;
                case CheckState.NOT_CONFIGURED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateNotConfigured"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionConfigure"));
                    item.ImageIndex = 1;
                    break;
                case CheckState.REMOVED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateRemoved"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionNothing"));
                    item.ImageIndex = 0;
                    break;
                case CheckState.NOT_REMOVED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateUninstall"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionRemove"));
                    item.ImageIndex = 1;
                    break;
                case CheckState.DOWNLOADED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateDownloaded"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionNothing"));
                    item.ImageIndex = 0;
                    break;
                case CheckState.NOT_DOWNLOADED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateNotDownloaded"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionDownload"));
                    item.ImageIndex = 1;
                    break;
                case CheckState.VERSION_MISMATCH:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateVersionMismatch"));
                    if (result.needsDownload)
                        item.SubItems.Add(Localizer.GetBestTranslation("Install_actionUninstallDownloadInstall"));
                    else
                    {
                        if (InstallationProperties.Instance["UpdateMode"] == "yes")
                        {
                            item.SubItems.Add(Localizer.GetBestTranslation("Install_actionUpgradeInstall"));
                        }
                        else
                        {
                            item.SubItems.Add(Localizer.GetBestTranslation("Install_actionUninstallInstall"));
                        }
                    }

                    item.ImageIndex = 2;
                    break;
                case CheckState.SKIPPED:
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_stateSkipped"));
                    item.SubItems.Add(Localizer.GetBestTranslation("Install_actionNothing"));
                    item.ImageIndex = 0;
                    break;
            }
        }

        public void PopulateListView()
        {
            listView.Items.Clear();

            AddPackageToListView(new DirectX9Checker());
            AddPackageToListView(new VCRedist2008Checker());

            if (InstallationProperties.Instance["DBMSType"] == "msSQL2005")
                AddPackageToListView(new MSSQLExpressChecker());
            if (InstallationProperties.Instance["DBMSType"] == "mysql")
                AddPackageToListView(new MySQLChecker());

            AddPackageToListView(new TvServerChecker());

            if ((InstallationProperties.Instance["ConfigureMediaPortalFirewall"] == "1" || InstallationProperties.Instance["ConfigureTVServerFirewall"] == "1") && InstallationProperties.Instance["InstallType"] != "download_only")
                AddPackageToListView(new WindowsFirewallChecker());

            listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private int PerformPackageAction(IInstallationPackage package, ListViewItem item)
        {
            //
            // return 0: nothing to do
            //        1: install successufull
            //        2: install error
            //
            CheckResult result = package.CheckStatus();
            if (result.state != CheckState.INSTALLED &&
                result.state != CheckState.REMOVED &&
                result.state != CheckState.DOWNLOADED &&
                result.state != CheckState.SKIPPED)
            {
                item.UseItemStyleForSubItems = false;
                item.SubItems[1].Font = new Font(item.SubItems[1].Font, FontStyle.Bold);
                switch (result.state)
                {
                    case CheckState.NOT_INSTALLED:
                        if (result.needsDownload)
                        {
                            item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgDownloading");
                            listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            listView.Update();
                            if (!package.Download())
                            {
                                Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errInstallFailed"), package.GetDisplayName()));
                                return 2;
                            }
                        }
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgInstalling");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.Install())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errInstallFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        break;

                    case CheckState.NOT_CONFIGURED:
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgConfiguring");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.Install())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errConfigureFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        break;

                    case CheckState.NOT_REMOVED:
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgUninstalling");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.Install())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errRemoveFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        break;

                    case CheckState.VERSION_MISMATCH:
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgUninstalling");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.UnInstall())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errUinstallFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        if (result.needsDownload)
                        {
                            item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgDownloading");
                            listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            listView.Update();
                            if (!package.Download())
                            {
                                Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errDownloadFailed"), package.GetDisplayName()));
                                return 2;
                            }
                        }
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgInstalling");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.Install())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errInstallFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        break;

                    case CheckState.NOT_DOWNLOADED:
                        item.SubItems[1].Text = Localizer.GetBestTranslation("Install_msgDownloading");
                        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                        listView.Update();
                        if (!package.Download())
                        {
                            Utils.ErrorDlg(string.Format(Localizer.GetBestTranslation("Install_errDownloadFailed"), package.GetDisplayName()));
                            return 2;
                        }
                        break;
                }
                return 1;
            }
            return 0;
        }

        public bool InstallComponents()
        {
            bool result = true;

            foreach (ListViewItem item in listView.Items)
            {
                IInstallationPackage package = (IInstallationPackage)item.Tag;
                int action = PerformPackageAction(package, item);
                item.UseItemStyleForSubItems = false;
                item.SubItems[1].Font = new Font(item.SubItems[1].Font, FontStyle.Regular);
                listView.Update();
                if (action == 2)
                {
                    result = false;  //something went wrong
                    break;
                }
                if (action == 1)
                {
                    item.SubItems[1].Text = Localizer.GetBestTranslation("Install_stateInstalled");
                    item.SubItems[2].Text = Localizer.GetBestTranslation("Install_actionNothing");
                    item.ImageIndex = 0;
                }
            }

            PopulateListView();

            return result;
        }
    }
}
